<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="数据日期">
              <j-date placeholder="请选择数据日期" v-model="queryParam.bbpc"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="报表名称">
              <a-input v-model="queryParam.zwmc" placeholder="请输入报表名称"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px;display:none">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
<!--      <a-button @click="myhandleVerification()" type="primary" icon="check-square" >校验</a-button>-->
      <a-button @click="myhandleCheckResult()" type="primary" icon="download"  >下载校验结果</a-button>
<!--      <a-button @click="myhandleCommitSJ()" type="primary" icon="check" >提交</a-button>-->
<!--      <a-dropdown v-if="selectedRowKeys.length > 0">-->
<!--        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
<!--      </a-dropdown>-->
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;display:none">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="guid"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleInfo(record.bbCode,record.bbpcs)"
             v-if="record.commitStatus==null || record.commitStatus !== '3'">复制上一期</a>
          <a
            :href="`/websheet/api?parameter=${Base.encode(
              `bbdm:${record.bbdm}&bbpc:${record.bbpc}&ssjg:${record.ssjg}&frdm:${record.frdm}&type:render&readonly:0`
            )}`"
            target="_blank"
            v-if="record.commitStatus==null || record.commitStatus !== '3'"
          >
            修改
          </a>
          <a
            :href="`/websheet/api?parameter=${Base.encode(
              `bbdm:${record.bbdm}&bbpc:${record.bbpc}&ssjg:${record.ssjg}&frdm:${record.frdm}&type:render&readonly:1`
            )}`"
            target="_blank"
          >
            查看
          </a>
          <a @click="myhandleCheck(record)"
             v-if="record.commitStatus==null || record.commitStatus !== '3'">校验</a>&nbsp;
          <a @click="myhandleExcel(record)"
             v-if="record.commitStatus==null || record.commitStatus !== '3'">下载校验结果</a>&nbsp;
          <a @click="myhandleApprove(record)"
             v-if="record.commitStatus==null || record.commitStatus !== '3'">提交</a>
        </span>
      </a-table>
    </div>
    <HandelCheckModal ref="handelCheck" @ok="modalFormOk"></HandelCheckModal>
  </a-card>
</template>

<script>
import '@assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HandelCheckModal from '../../checkData/HandelCheckModal'
import { Base } from '@/views/websheet/luckysheet/utils/utils.js'
import { downFile, getAction } from '@/api/manage'
import { axios } from '@/utils/request'

export default {
  name: 'StaticsList',
  mixins:[JeecgListMixin, mixinDevice],
  components: {HandelCheckModal
  },
  data () {
    return {
      Base,
      description: '金融信息统计页面',
      // 表头
      columns: [
        {
          title: '选项',
          dataIndex: '',
          key:'rowIndex',
          width:60,
          align:"center",
          customRender:function (t,r,index) {
            return parseInt(index)+1;
          }
        },
        {
          title:'数据日期',
          align:"center",
          dataIndex: 'bbpc'
        },
        {
          title:'报表代码',
          align:"center",
          dataIndex: 'bbdm'
        },
        {
          title:'报表名称',
          align:"center",
          dataIndex: 'zwmc'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align:"center",
          fixed:"right",
          width:147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: "/bulu/statics/list",
        delete: "/bulu/statics/delete",
        deleteBatch: "/bulu/statics/deleteBatch",
        exportXlsUrl: "/bulu/statics/exportXls",
        importExcelUrl: "bulu/statics/importExcel",
        dataCheckUrl: "/bulu/statics/dataCheck",
        dataCommitUrl: "/bulu/statics/dataCommit",
        dataVerification: '/bulu/statics/dataVerification',
        exportCheckErrorDataXlsUrl:'/bulu/statics/exportVerificationErrorDataXls',

      },
      dictOptions:{},
      superFieldList:[],
    }
  },
  created() {
    this.getSuperFieldList();
  },
  computed: {
    importExcelUrl: function(){
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
    },
  },
  methods: {
    initDictConfig(){
    },
    getSuperFieldList(){
      let fieldList=[];
      fieldList.push({type:'string',value:'zwmc',text:'报表名称'})
      fieldList.push({type:'string',value:'bbdm',text:'报表代码'})
      fieldList.push({type:'string',value:'bbpc',text:'报送批次'})
      fieldList.push({type:'string',value:'ssjg',text:'所属机构'})
      fieldList.push({type:'string',value:'frdm',text:'法人代码'})
      this.superFieldList = fieldList
    },
    myhandleVerification(){
      if(this.selectedRowKeys.length<1){
        this.$message.warning("没有选中的数据");
        return ;
      }
      if(this.selectedRowKeys.length>1){
        this.$message.warning("只能选择一个数据");
        return ;
      }
      var dates=this.dataSource[this.selectedRowKeys[0]]['bbpcs'];
      var codes=this.dataSource[this.selectedRowKeys[i]]['bbdm'];
      jhandleCheckResultInfo(codes,dates);
    },
    myhandleCheckResult(){
      if(this.selectedRowKeys.length<1){
        this.$message.warning("没有选中的数据");
        return ;
      }
      var that=this;
      for (let i = 0; i < that.selectedRowKeys.length; i++) {
        let fileName = "校验结果表";
        let downloadUrl = '/verificationresult/llbbVerificationResult/exportXls';
        let downloadParam = {"sjrq":that.dataSource[this.selectedRowKeys[i]]['bbpcs'],
          "reportCode":that.dataSource[this.selectedRowKeys[i]]['bbdm']};
        downFile(downloadUrl,downloadParam).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return;
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      }
    },
    downFile(url,parameter){
      return axios({
        url: url,
        params: parameter,
        method:'get' ,
        responseType: 'blob'
      })
    },
    myhandleCommitSJ(){
      if(this.selectedRowKeys.length<1){
        this.$message.warning("没有选中的数据");
        return ;
      }
      var dates="";
      var codes="";
      for (let i = 0; i < this.selectedRowKeys.length; i++) {
        dates+=this.dataSource[this.selectedRowKeys[i]]['bbpc']+",";
        codes+=this.dataSource[this.selectedRowKeys[i]]['bbdm']+",";
      }
      dates=dates.substring(0,dates.length-1);
      codes=codes.substring(0,codes.length-1);
      var param={'dates':dates,'codes':codes};
      var that=this;
      getAction("/bulu/statics/dataCommit", param).then((res) => {
        if (res.success) {
          that.$message.warning("提交成功");
          that.loadData();
        } else {
          that.$message.warning("提交失败");
        }
      })
    },
    myhandleCheck(record){
      var dates=record.bbpc;
      var codes=record.bbdm;
      var param={'dates':dates,'codes':codes};
      var that=this;
      getAction("/bulu/statics/dataCheck", param).then((res) => {
        if (res.success) {
          that.$message.warning("校验成功");
          that.loadData();
        } else {
          that.$message.warning("校验失败");
        }
      })
    },
    myhandleExcel(record){
      let fileName = "校验结果表";
      let downloadUrl = '/verificationresult/llbbVerificationResult/exportXls';
      let downloadParam = {"sjrq":record['bbpcs'],"reportCode":record['bbdm']};
      downFile(downloadUrl,downloadParam).then((data)=>{
        if (!data) {
          this.$message.warning("文件下载失败")
          return;
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data],{type: 'application/vnd.ms-excel'}), fileName+'.xls')
        }else{
          let url = window.URL.createObjectURL(new Blob([data],{type: 'application/vnd.ms-excel'}))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName+'.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      })
    },
    myhandleApprove(record){
      var dates=record.bbpc;
      var codes=record.bbdm;
      var param={'dates':dates,'codes':codes};
      var that=this;
      getAction("/bulu/statics/dataCommit", param).then((res) => {
        if (res.success) {
          that.$message.warning("提交成功");
          that.loadData()
        } else {
          that.$message.warning("提交失败");
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
